package com.msb.dongbao.portal.web.controller.studyCaptcha;


import com.msb.dongbao.common.base.annotations.TokenCheck;
import com.msb.dongbao.portal.web.util.JCaptchaUtil;
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

@RestController
@RequestMapping("/jcaptcha")
public class JCaptchaController {

    String arrtName = "verifyCode";


    @GetMapping("/generator")
    @TokenCheck(required = false)
    public void generateCode(HttpServletRequest request, HttpServletResponse response) {
//        try {
//            String id = request.getSession().getId();
//            BufferedImage bufferedImage = JCaptchaUtil.getService().getImageChallengeForID(id);//session id
//            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream);
//            jpegEncoder.encode(bufferedImage);
//            response.setHeader("Cache-Control", "no-store");
//            response.setContentType("image/jpeg");
//            byte[] bytes = byteArrayOutputStream.toByteArray();
//            ServletOutputStream out = response.getOutputStream();
//            out.write(bytes);
//            out.flush();
//            out.close();
//        } catch (Exception e) {
//
//        }

    }


    @GetMapping("/verify")
    @TokenCheck(required = false)
    public String verifty(String verifyCode, HttpServletRequest request) {

        Boolean aBoolean = JCaptchaUtil.getService().validateResponseForID(request.getSession().getId(), verifyCode);
        if (aBoolean) {
            return "通过";
        }
        return "不通过";
    }
}
